package deom2;

import java.util.Arrays;

public class MyArrayList {
    //创建数组作为顺序表
    private int[] array;
    //数组中元素的个数
    private int useSize;

    MyArrayList() {
        //设置初始容量
        array = new int[10];
    }

    /*
    * 打印
    * */
    public void print() {
        for (int i = 0; i < useSize; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    /*
    * 求数组元素的个数
    * */
    public int size() {
        return this.useSize;
    }

    /*
    * 扩容
    * */
    private void expanCapacity() {
        this.array = Arrays.copyOf(array,array.length * 2);
    }

    /*
    * 尾插
    * */
    public void addLast(int val) {
        if (useSize == array.length) {
            expanCapacity();
        }
        array[useSize] = val;
        useSize++;
    }

    /*
    * 头插
    * */
    public void addFont(int val) {
        if (useSize == array.length) {
            expanCapacity();
        }
        int cur = useSize;
        int i = cur;
        for (; i > 0 ; i--) {
            array[i] = array[i - 1];
        }
        array[i] = val;
        useSize++;
    }

    /*
    * 在指定位置 pos 插入 val
    * */
    public void addPos(int pos,int val) {
        if (pos < 0 || pos > useSize) {
            System.out.println("Pos is false!");
            return;
        }
        if (useSize == array.length) {
            expanCapacity();
        }
        if (pos == 0) {
            addFont(val);
            return;
        }
        if (pos == useSize) {
            addLast(val);
            return;
        }
        int cur = useSize;
        int i = cur;
        for (; i > pos; i--) {
            array[i] = array[i - 1];
        }
        array[i] = val;
        useSize++;
    }

    /*
    * 删除第一个 key 的元素
    * */
    public void remove(int key) {
        if (!contain(key)) {
            System.out.println("Key is no!");
            return;
        }
        int cur = 0;
        for (int i = 0; i < useSize; i++) {
            if (array[i] == key) {
                cur = i;
                break;
            }
        }
        for (int j = cur; j < useSize - 1; j++) {
            array[j] = array[j + 1];
        }
        useSize--;
    }

    /*
    * 删除全部 key 的元素
    * */
    public void removeAll(int key) {
        if (!contain(key)) {
            System.out.println("Key is no!");
        }
        for (int i = 0; i < useSize; i++) {
            if (key == array[i]) {
                for (int j = i; j < useSize - 1; j++) {
                    array[j] = array[j + 1];
                }
                useSize--;
                i--;
            }
        }
    }

    /*
    * 是否包含 key 的元素
    * */
    public boolean contain(int key) {
        for (int i = 0; i < useSize; i++) {
            if (key == array[i]) {
                return true;
            }
        }
        return false;
    }

    /*
    * 把 key 的元素修改为 val
    * */
    public void setVal(int key,int val) {
        if (!contain(key)) {
            System.out.println("Key is no!");
        }
        for (int i = 0; i < useSize; i++) {
            if (array[i] == key) {
                array[i] = val;
            }
        }
    }
}
